package com.lxhdj.management.controller;

import com.lxhdj.common.annotation.ApiLog;
import com.lxhdj.common.model.TestModel;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.io.OutputStream;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @GetMapping("/restr")
    @ApiLog
    public String restr(String a, Integer b){

        return "sdf"+a+b;
    }
    @GetMapping("/revoid")
    @ApiLog
    public void revoid(){

    }
    @GetMapping("/reint")
    @ApiLog
    public Integer reint(){
        return 1;
    }
    @PostMapping("/reobj")
    @ApiLog("b")
    public  TestModel reobj(@RequestBody TestModel testModel){

        return testModel;
    }

    @GetMapping("/refile")
    @ApiLog("refile")
    public void  downloadExcelTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
        ClassPathResource resource = new ClassPathResource("templates/1.xlsx");
        InputStream in =resource.getInputStream();
        String fileName = "parameterTemplate.xls";

        int len ;
        byte[] buffer = new byte[1024];
        response.setHeader("content-disposition", String.format("attachment; filename=%s", fileName));
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

        OutputStream out = response.getOutputStream();
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.flush();
        in.close();
    }
    @GetMapping ("/reep")
    public  String reep(String name) throws Exception {
        if(name.isEmpty()){
            throw new Exception("error");
        }
        return name;
    }
}
